/**
 * uni-app API 适配器
 * 用于在Vue项目中模拟uni-app API的功能
 */
import router from '../router/index'

// 全局uni对象
const uni = {
  // 导航相关API
  navigateTo(options) {
    if (!options || !options.url) {
      console.error('navigateTo: url is required')
      return
    }
    // 移除开头的斜杠，因为vue-router不需要
    const url = options.url.charAt(0) === '/' ? options.url.substr(1) : options.url
    router.push(url)
    if (typeof options.success === 'function') {
      options.success()
    }
  },
  
  redirectTo(options) {
    if (!options || !options.url) {
      console.error('redirectTo: url is required')
      return
    }
    const url = options.url.charAt(0) === '/' ? options.url.substr(1) : options.url
    router.replace(url)
    if (typeof options.success === 'function') {
      options.success()
    }
  },
  
  navigateBack(options = {}) {
    const delta = options.delta || 1
    router.go(-delta)
    if (typeof options.success === 'function') {
      options.success()
    }
  },
  
  switchTab(options) {
    if (!options || !options.url) {
      console.error('switchTab: url is required')
      return
    }
    const url = options.url.charAt(0) === '/' ? options.url.substr(1) : options.url
    router.push(url)
    if (typeof options.success === 'function') {
      options.success()
    }
  },
  
  // 界面相关API
  showToast(options) {
    // 可以使用vant的Toast或其他UI库的提示组件
    if (window.$toast) {
      window.$toast(options.title || '')
    } else {
      alert(options.title || '')
    }
    if (typeof options.success === 'function') {
      options.success()
    }
  },
  
  showLoading(options) {
    if (window.$loading) {
      window.$loading.show(options.title || '加载中...')
    }
    if (typeof options.success === 'function') {
      options.success()
    }
  },
  
  hideLoading(options = {}) {
    if (window.$loading) {
      window.$loading.hide()
    }
    if (typeof options.success === 'function') {
      options.success()
    }
  },
  
  // 数据缓存API
  setStorage(options) {
    try {
      localStorage.setItem(options.key, JSON.stringify(options.data))
      if (typeof options.success === 'function') {
        options.success()
      }
    } catch (e) {
      if (typeof options.fail === 'function') {
        options.fail(e)
      }
    }
  },
  
  getStorage(options) {
    try {
      const value = localStorage.getItem(options.key)
      if (value) {
        if (typeof options.success === 'function') {
          options.success({ data: JSON.parse(value) })
        }
      } else {
        if (typeof options.fail === 'function') {
          options.fail({ errMsg: 'getStorage:fail' })
        }
      }
    } catch (e) {
      if (typeof options.fail === 'function') {
        options.fail(e)
      }
    }
  },
  
  removeStorage(options) {
    try {
      localStorage.removeItem(options.key)
      if (typeof options.success === 'function') {
        options.success()
      }
    } catch (e) {
      if (typeof options.fail === 'function') {
        options.fail(e)
      }
    }
  },
  
  clearStorage(options = {}) {
    try {
      localStorage.clear()
      if (typeof options.success === 'function') {
        options.success()
      }
    } catch (e) {
      if (typeof options.fail === 'function') {
        options.fail(e)
      }
    }
  }
}

// 将uni对象挂载到全局
window.uni = uni

export default uni